package cn.yanhu.kuwanapp.common.activity

import android.content.Intent
import android.os.Bundle
import android.view.WindowManager
import androidx.activity.viewModels
import androidx.lifecycle.Observer
import cn.woochen.comlib.BaseActivity
import cn.woochen.comlib.util.UserPref
import cn.yanhu.kuwanapp.R
import cn.yanhu.kuwanapp.common.viewmodels.CommonViewModel
import cn.yanhu.kuwanapp.databinding.ActivitySplashBinding
import cn.yanhu.kuwanapp.home.MainActivity
import cn.yanhu.kuwanapp.user.LoginActivity
import cn.yanhu.kuwanapp.utils.constans.Constants

/**
 * 闪屏页
 */
class SplashActivity : BaseActivity<ActivitySplashBinding, CommonViewModel>() {

    private var mToken by UserPref(Constants.USER_TOKEN, "")

    override fun setContentView(): Int = R.layout.activity_splash

    override fun initData() {
        mViewModel.eventTrace(Constants.EVENT_KEY_APP_LAUNCH)
        if (intent.flags and Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT != 0) {
            finish()
            return
        }
        window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
    }

    override fun registerNecessaryObserver() {
        super.registerNecessaryObserver()
        //埋点
        mViewModel.eventTraceInfo.observe(this, Observer {
            toMainPage()
        })
    }

    /**
     * 去首页
     */
    private  fun toMainPage(){
        if (mToken.isBlank()){
            LoginActivity.start(this)
        }else{
            MainActivity.start(this)
        }
        finish()
    }


}